<?php


namespace app\index\controller;

use app\common\controller\ArticalCm;
use app\common\controller\LogsCm;
use app\index\model\Pass;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\Request;
use app\index\model\Lists;


/**
 * Class Message
 * @package app\index\controller
 */
class Message extends BaseController
{
    /**
     * @return mixed
     */
    public function index()
    {
        return $this->fetch();
    }

    //私密页面

    /**
     * @return mixed
     */
    public function privatePage()
    {
        return $this->fetch('private', ['private' => 0]);
    }

    //审核页面

    /**
     * @return mixed
     */
    public function checkMes()
    {
        return $this->fetch();
    }

    //用户管理

    /**
     * @return mixed
     */
    public function userMan()
    {
        return $this->fetch();
    }

    /**
     * 关闭回复功能
     * @return mixed
     * @throws \think\exception\PDOException
     */
    public function closeReply()
    {
        $data['status'] = 1;
        $data['msg'] = '操作失败';
        //参数
        $id = $this->request->param('id');
        $state = $this->request->param('state');

        $cdn = [];
        if ($state != '') {
            $cdn = ['op_auth' => $state];
        }
        $article_ob = new ArticalCm();
        if ($article_ob->updateArticleState($id, $cdn)) {
            $data['status'] = 0;
            $data['msg'] = '操作成功';
        }

        $content = $this->request->param();
        //写入日志
        LogsCm::WriteAdmLog($this->username, '回复开关：' . json_encode($content));
        return $data;

    }

    /**
     * 开启关闭私密功能
     * @return mixed
     * @throws \think\exception\PDOException
     */
    public function closePrivate()
    {
        $data['status'] = 1;
        $data['msg'] = '操作失败';
        //参数
        $id = $this->request->param('id');
        $private = $this->request->param('private');

        $cdn = [];
        if ($private != '') {
            $cdn = ['private' => $private];
        }
        $article_ob = new ArticalCm();
        if ($article_ob->updateArticleState($id, $cdn)) {
            $data['status'] = 0;
            $data['msg'] = '操作成功';
        }

        $content = $this->request->param();
        //写入日志
        LogsCm::WriteAdmLog($this->username, '回复开关：' . json_encode($content));
        return $data;

    }

    /**
     *查询留言板
     * @param Request $request
     * @return mixed
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @throws \think\Exception
     * @author LeeBW
     * @date 2020-12-16 15:50
     */
    public function read(Request $request)
    {

        $id = $request->param('id');
        $name = $request->param('name');
        $article = Lists::get($id);
        $article->content = htmlspecialchars_decode($article->content);
        $comments = $article->comments()->select();
        $level = Db::name('user')->where('username', $name)->value('level');
        $article_user = Db::name('article')->where('id',$id)->value('username');
        $level_mes = Db::name('user')->where('username',$article_user)->value('level_mes');

        $this->assign('article', $article);
        $this->assign('comments', $comments);
        $this->assign('level', $level);
        $this->assign('level_mes', $level_mes ?$level_mes:0);

        return $this->fetch();
    }


    /**
     * @param Request $request
     * @return mixed
     */
    public function passArticle(Request $request)
    {
        $id = $request->param('id');
        $pass_auth = $request->param('pass_auth');
        $data['status'] = 1;
        $return = ArticalCm::updateArticleState($id, ['pass_auth' => $pass_auth]);
        if ($return == 1) {
            $data['status'] = 0;
            $data['msg'] = '操作成功';
        } else {
            $data['msg'] = '操作失败！';

        }
        return $data;

    }

    /**
     * 查询用户信息
     * @return mixed
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getUserInfo()
    {
        $user = $this->request->param('username', '');
        $username = $this->request->param('user', '');
        $start = $this->request->param('start', '');
        $limit = $this->request->param('limit', '');
        $cdn = [];
        if ($start == '')
            $start = 0;

        if (empty($limit)) {

            $limit = 50;
        }
        if (!empty($user)) {
            $cdn['username'] = ['LIKE', "%$user%"];
        }
        $cdn['username'] = ['NEQ',$username];
//        $return = Pass::all();
//
//        if($return) {
//            $return = collection($return)->toArray();
//        }
        $return = Db::name('user')->where($cdn)->limit($start, $limit)->select();
        $all_data = count($return);
        // 总页数
        $lock_total_page = ceil($all_data / $limit);
        if ($lock_total_page < 1) {
            $lock_total_page = 1;
        }


        $level = Db::name('user')->where('username', $this->username)->value('level');
        $data['level'] = true;
        if (isset($level) && $level == 0) {
            $data['level'] = false;
            $data['auth_level'] = 0;
        }
        $data['status'] = 0;
        $data['msg'] = '查询成功!';
        $data['list'] = !empty($return) ? $return : [];
        $data['page']['total_page'] = !empty($lock_total_page) ? $lock_total_page : 0;
        $data['page']['total_num'] = $all_data;
        return $data;
    }

    /**
     * @return mixed
     * @throws DbException
     */
    public function deleteUser()
    {
        $id = $this->request->param('user_id', '');

        $user_ob = Pass::get($id);
        if ($user_ob) {
            $array = json_encode($user_ob->toArray(), true);
            $user_ob->delete();
            $data['status'] = 0;
            $data['msg'] = '删除成功！';
            LogsCm::WriteAdmLog($this->username, '删除用户：' . $array);

            return $data;
        } else {
            $data['status'] = 1;
            $data['msg'] = '没有删除的记录！';

            return $data;
        }

    }

    /**
     * 认证用户
     * @return mixed
     */
    public function passPeople()
    {
        $id = $this->request->param('user_id', '');

        $return = Pass::where('user_id', $id)->update(['condition' => 0]);
        if ($return) {
            $data['status'] = 0;
            $data['msg'] = '操作成功！';
        } else {
            $data['status'] = 1;
            $data['msg'] = '没有删除的记录！';
        }
        return $data;

    }

    /**
     * 修改用户信息
     * @return mixed
     */
    public function changeUser()
    {
        $username = $this->request->param('username');
        $email = $this->request->param('email');
        $nick_name = $this->request->param('nick_name');
        $user_id = $this->request->param('id', '');

        if($username)
        {
            $cdn['username'] = $username;
        }
        if($email)
        {
            $cdn['email'] = $email;
        }
        if($nick_name)
        {
            $cdn['nick_name'] = $nick_name;
        }
       $return = Pass::where('user_id',$user_id)->update($cdn);
       if ($return) {
            $data['status'] = 0;
            $data['msg'] = '操作成功！';
        } else {
            $data['status'] = 1;
            $data['msg'] = '修改失败！';
        }
        return $data;

    }
}